package cn.zmit.kuxi.activity;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.common.utils.ToastUtils;
import com.xdroid.functions.holder.ListViewDataAdapter;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;
import com.xdroid.widget.loadmore.LoadMoreContainer;
import com.xdroid.widget.loadmore.LoadMoreDefaultFooterView;
import com.xdroid.widget.loadmore.LoadMoreHandler;
import com.xdroid.widget.loadmore.LoadMoreListViewContainer;
import com.xdroid.widget.ptr.PtrDefaultHandler;
import com.xdroid.widget.ptr.PtrFrameLayout;
import com.xdroid.widget.ptr.PtrHandler;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.entity.JoinRecordEntity;
import cn.zmit.kuxi.holder.JoinRecordHolder;
import cn.zmit.kuxi.request.RequestTask;
import cn.zmit.kuxi.widget.PtrHeader;

/** * @author chenxu @parameter * @return */
public class AlreadyJoinrecordActivity extends BaseActivity {
	@ViewInject(R.id.list_goods)
	private ListView list_goods;
	private ListViewDataAdapter<JoinRecordEntity> joinrecordAdapter;
	public String userId = null, flag = null;
	String winId;
	/**
	 * 页码
	 */
	private int mPage = 1;
	@ViewInject(R.id.load_more_list_view_container)
	private LoadMoreListViewContainer mLoadMoreListViewContainer;

	// 下拉刷新
	@ViewInject(R.id.ptr_view)
	private PtrFrameLayout ptr_view;
	private PtrHeader ptrHeader;
	private LoadMoreDefaultFooterView mFooterView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleView("TA的个人中心", true);
		View view = setContentViewRe(R.layout.fragment_goods);
		ViewUtils.inject(this);
		initView();
		winId = getTextFromBundle("win_id");
		initPtr();
		initLoadMore();
		Button btn_gotop = (Button) view.findViewById(R.id.btn_gotop);
		btn_gotop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				list_goods.setSelection(0);
				initPtr();
			}
		});
	}

	/**
	 * 初始化上拉刷新，分页显示
	 */
	private void initLoadMore() {
		mFooterView = mLoadMoreListViewContainer.useDefaultFooterRe();
		mLoadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {

			@Override
			public void onLoadMore(LoadMoreContainer loadMoreContainer) {
				if (!mLoadMoreListViewContainer.isLoadError()) {
					mPage++;
					getData();
				}
			}
		});
	}

	/**
	 * 初始化下拉刷新
	 */
	private void initPtr() {
		/* 创建自定义刷新头部view */
		ptrHeader = new PtrHeader(this);
		/* 设置刷新头部view */
		ptr_view.setHeaderView(ptrHeader);
		/* 设置回调 */
		ptr_view.addPtrUIHandler(ptrHeader);
		/* 横向拉动时，禁止下拉 */
		ptr_view.disableWhenHorizontalMove(true);
		// 设置监听
		ptr_view.setPtrHandler(new PtrHandler() {

			@Override
			public void onRefreshBegin(PtrFrameLayout frame) {
				frame.postDelayed(new Runnable() {

					@Override
					public void run() {
						mFooterView.setVisibility(View.GONE);
						joinrecordAdapter.removeAll();
						mPage = 1;
						getData();

					}
				}, 500);
			}

			@Override
			public boolean checkCanDoRefresh(PtrFrameLayout frame,
					View content, View header) {
				// TODO Auto-generated method stub
				return PtrDefaultHandler.checkContentCanBePulledDown(frame,
						list_goods, header);
			}
		});
		/* 延时100hao秒,自动刷新 */
		ptr_view.postDelayed(new Runnable() {

			@Override
			public void run() {
				ptr_view.autoRefresh();
			}
		}, 100);
	}

	private void getData() {
		RequestTask.getInstance().getHistoryRecord(this, winId, mPage + "",
				new onRequestListener());// 请求
	}

	private void initView() {// 初始化
		joinrecordAdapter = new ListViewDataAdapter<JoinRecordEntity>();
		joinrecordAdapter.setViewHolderClass(this, JoinRecordHolder.class);
		list_goods.setAdapter(joinrecordAdapter);
	}

	// 参与记录
	private class onRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			super.onDone(request, response, dataType);
			Boolean mIsEmpty = false; // 是否是空数据
			Boolean mHasMore = false; // 是否有更多数据待加载
			JsonData jsonData = JsonData.create(response);
			JsonData data = jsonData.optJson("winner_buy_record");
//			String now_time="";
			if (data != null && data.length() > 0) {
				if (data.length() > 0) {
					if (data.length() == 8) {
						mHasMore = true; // 有更多数据
					} else if (data.length() < 8) {
						mIsEmpty = false; // 有数据
						mHasMore = false; // 无更多数据
					}
					for (int i = 0; i < data.length(); i++) {
						JsonData itemData = data.optJson(i);
						String buy_count = itemData.optString("buy_count");
						String name = itemData.optString("name");
						String title = itemData.optString("phase");
						String winner_id = itemData.optString("winner_id");
						String time = itemData.optString("announceOn");
						String lucky_number = itemData
								.optString("lucky_number");
						String pic = itemData.optString("pic");
						JoinRecordEntity joinRecordEntity = new JoinRecordEntity(
								buy_count, name, title, winner_id, time,
								lucky_number, pic);
						joinrecordAdapter.append(joinRecordEntity);
					}
				} else {
					mIsEmpty = true; // 空数据
					mHasMore = false; // 无更多数据
				}
				mLoadMoreListViewContainer.loadMoreFinish(mIsEmpty, mHasMore); // 判断是否加载更多
			}
			ptr_view.refreshComplete();
		}

		@Override
		public void onRequestPrepare() {
			super.onRequestPrepare();
		}

		@Override
		public void onRequestFailed(String error) {
			super.onRequestFailed(error);
		}
	}
}
